package response

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

const SUCCESS = 100200
const ERR_NOT_LOGIN = 100401
const ERR_NOT_FOUND = 100404
const ERR_PARAMETER = 100403
const ERR_STATUS = 100404
const ERR_CUSTOM = 100405
const ERR_NOT_FOUND_CODE = 100500
const ERR_USER_EXISTED = 100406
const ERR_YZM_CODE = 100407
const ERR_AUTHORITY = 100408
const ERR_UNSETTLED_URL = 100409

type Response struct {
	Code int         `json:"code"`
	Msg  interface{} `json:"msg"`
	Data interface{} `json:"data,omitempty"`
}

func Result(code int, msg interface{}, data interface{}, c *gin.Context) {
	// 开始时间
	c.JSON(http.StatusOK, &Response{
		code,
		msg,
		data,
	})
}

func OkWithMessage(message interface{}, data interface{}, c *gin.Context) {
	Result(SUCCESS, message, data, c)
}

func FailWithMessage(message interface{}, c *gin.Context) {
	Result(ERR_PARAMETER, message, nil, c)
}

///////////////////////////////////////////////////////////////

type ResponseData struct {
	Code ResCode     `json:"code"`
	Msg  interface{} `json:"msg"`
	Data interface{} `json:"data,omitempty"`
}

func ResponseErr(c *gin.Context, code ResCode) {
	c.JSON(http.StatusOK, &ResponseData{
		Code: code,
		Msg:  code.Msg(),
		Data: nil,
	})

}

func ResponseErrorWithMsg(c *gin.Context, code ResCode, msg interface{}) {
	c.JSON(http.StatusOK, &ResponseData{
		Code: code,
		Msg:  msg,
		Data: nil,
	})
}

func ResponseSuccess(c *gin.Context, data interface{}) {
	c.JSON(http.StatusOK, &ResponseData{
		Code: CodeSuccess,
		Msg:  CodeSuccess.Msg(),
		Data: data,
	})

}
